package com.test.mchelepesa.dialog_rice;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.test.mchelepesa.R;
import com.test.mchelepesa.adapter_rice.AccountChannelAdapter;
import com.test.mchelepesa.bean_rice.StaticConfig_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.listener.OnBindAccountListener;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.view_rice.RiceEditText;

public class BindAccountDialog_rice extends Dialog {

    private final OnBindAccountListener onBindAccountListener;

    @SuppressLint("SetTextI18n")
    public BindAccountDialog_rice(@NonNull Context context, OnBindAccountListener onBindAccountListener) {
        super(context);
        this.onBindAccountListener = onBindAccountListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bind_account_rice);

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setCanceledOnTouchOutside(false);

        findViewById(R.id.dialog_bind_account_close).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_BIND_ACCOUNT_CLOSE_CLICK");
            dismiss();
        });

        RecyclerView channelRecycler = findViewById(R.id.dialog_bind_account_channel_recycler);
        TextView areaCodeTv1 = findViewById(R.id.dialog_bind_account_area_code_1);
        TextView areaCodeTv2 = findViewById(R.id.dialog_bind_account_area_code_2);
        RiceEditText mobileEt1 = findViewById(R.id.dialog_bind_account_et_mobile_1);
        RiceEditText mobileEt2 = findViewById(R.id.dialog_bind_account_et_mobile_2);

        mobileEt1.setBpType("DIALOG_BIND_ACCOUNT_MOBILE_1_INPUT");
        mobileEt2.setBpType("DIALOG_BIND_ACCOUNT_MOBILE_2_INPUT");

        areaCodeTv1.setText(StaticConfig_rice.getMobilePrefix());
        areaCodeTv2.setText(StaticConfig_rice.getMobilePrefix());

        AccountChannelAdapter channelViewAdapter = new AccountChannelAdapter(AppShowInfo_rice.getInstance().getWalletChannels());

        if (channelRecycler!=null) {
            channelRecycler.setAdapter(channelViewAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
            gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
            channelRecycler.setLayoutManager(gridLayoutManager);
        }

        findViewById(R.id.dialog_bind_account_btn_add).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_BIND_ACCOUNT_ADD_CLICK");

            String mobile1 = mobileEt1.getTextString();
            String mobile2 = mobileEt2.getTextString();

            if (TextUtils.isEmpty(mobile1) || TextUtils.isEmpty(mobile2)){
                Toast.makeText(getContext(),"Veuillez saisir le numéro de téléphone portable lié à la carte.",Toast.LENGTH_SHORT).show();
                return;
            }

            dismiss();

            if (onBindAccountListener !=null) onBindAccountListener.onBindAccount(mobile1,channelViewAdapter.getSelectChannelDesc());

            HttpManager_rice.getInstance().bindBankAccount(mobile1,mobile2,channelViewAdapter.getSelectChannelValue());

        });

    }

}
